const api = require('../../config/api.js'); 
const app = getApp();
let that;
Page({
  data: {
    RootUrl:api.RootUrl,
    ImgRootUrl: app.globalData.ImgRootUrl,
    newGoods: [],
    hotGoods: [],
    topics: [],
    brands: [],
    floorGoods: [],
    banner: [],
    channel: []
  },
  onLoad: function (options) {
    that = this;
    this.getIndexData();
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
    if (app.globalData.Refresh) {
      app.globalData.Refresh = false;
      this.getIndexData();
    }
    // 购物车角标
    if (app.globalData.cartGoodsCount != null) {
      app.cart.setCartBadge(app.globalData.cartGoodsCount);
    }else{
      app.cart.CartGoodsCount().then(function (res) {
        app.globalData.cartGoodsCount = res.data.cartTotal.goodsCount;
      }).catch((err) => {
        console.log(err)
      });
    }    
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.title,
      desc: app.globalData.desc,
      path: '/pages/index/index'
    }
  },
  getIndexData: function () {
    app.util.wxrequest(api.IndexUrl).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          newGoods: res.data.newGoodsList,
          hotGoods: res.data.hotGoodsList,
          //topics: res.data.topicList,
          brand: res.data.brandList,
          floorGoods: res.data.categoryList,
          banner: res.data.banner,
          channel: res.data.channel
        });
      }
    });
  },
})
